package cn.easybuy.servlet.order;

import cn.easybuy.entity.*;
import cn.easybuy.service.OrderService;
import cn.easybuy.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/addOrderServlet")
public class AddOrderServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User sessionUser = (User) req.getSession().getAttribute("sessionUser");
        List<BuyCar> listCarSession = (List<BuyCar>) req.getSession().getAttribute("listCarSession");
        int size = (int)req.getSession().getAttribute("size");
        int id = sessionUser.getId();
        String name = sessionUser.getUserName();
        String address = req.getParameter("address");
        String remark = req.getParameter("remark");
        String address2 = req.getParameter("address2");
        float sum = Float.parseFloat(req.getParameter("sum"));
        req.setAttribute("sum",sum);
        Order order = new Order();
        order.setUserId(id);
        order.setLoginName(name);
        order.setCost(sum);
        String serialNumber = order.getSerialNumber();
        order.setSerialNumber(serialNumber);
        try {
            if (address !="" && remark !=""){
                UserAddress userAddress = new UserAddress();
                userAddress.setUserId(id);
                userAddress.setAddress(address);
                userAddress.setRemark(remark);
                boolean b = orderService.AddUserAddress(userAddress);
                if (b){
                    order.setUserAddress(address);
                }
            }else {
                order.setUserAddress(address2);
            }
            boolean b = orderService.AddOrder(order);
            if (b){
                int i = orderService.selOrder(serialNumber);
                for (int j = 0; j < size; j++) {
                    int id1 = listCarSession.get(j).getId();
                    int num = listCarSession.get(j).getNum();
                    float sum1 = listCarSession.get(j).getSum();
                    OrderDetail orderDetail = new OrderDetail();
                    orderDetail.setOrderId(i);
                    orderDetail.setProductId(id1);
                    orderDetail.setQuantity(num);
                    orderDetail.setCost(sum1);
                    orderService.AddDatail(orderDetail);
                }
                req.setAttribute("name",name);
                req.setAttribute("address",address);
                req.setAttribute("serialNumber",serialNumber);
                req.getRequestDispatcher("pre/settlement/settlement2.jsp").forward(req,resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}